<template>
	<view class="content">
		<view class="title">
			<text class="iconfont icon-shop"></text>
			<text>优购生活馆</text>
		</view>
		<!-- 商品列表 -->
		<view class="goods-list">
			<view class="goods-item" v-for="(item, index) in goodsList" :key="index" @click="toItem(item.goodsId)">
				<text class="iconfont" :class="item.checked?'icon-checked':'icon-unchecked'" @click.stop="toggleChecked(item)"></text>
				<image :src="item.goods_small_logo" alt="">
				</image>
				<view class="right">
					<!-- //item.goodsId去goodsList找一个符合条件的对象,条件对象.goods_id===item.goodsId,然后取对象.goods_name -->
					<text class="text-line2">{{item.goods_name}}</text>
					<view class="btm">
						<text class="price">
							￥
							<text>{{item.goods_price}}</text>
							.00
						</text>
						<view class="goods-num">
							<button @click.stop="subNum(item,index)">-</button>
							<text>{{item.num}}</text>
							<button @click.stop="addNum(item)">+</button>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="account">
			<view class="select-all" @click="toggleAll">
				<text class="iconfont" :class="isAll?'icon-checked':'icon-unchecked'"></text>
				<text>全选</text>
			</view>
			<view class="price-wrapper">
				<view class="price">
					<view>
						合计:
						<text class="num">￥{{totalPrice}}.00</text>
					</view>
					<text class="info">包含运费</text>
				</view>
			</view>
			<view class="account-btn" @click="toPay">结算({{totalNum}})</view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				// storageCartArr: [],
				goodsList: []
			}
		},
		// 加入购物车,再切换到购物车页面,要刷新
		onShow() {
			// 商品id以逗号分隔
			// this.storageCartArr = uni.getStorageSync('cart') || []
			this.storageCartArr = this.$store.state.cart

			// let idStr = ''
			// storageCartArr.forEach(item=>{
			// 	idStr+=item.goodsId+','
			// })
			// 不知道怎么去掉最后这个逗号

			// goodsId的数组，然后join
			let goodsIdArr = this.storageCartArr.map(item => {
				return item.goodsId
			})

			let goodsIdStr = goodsIdArr.join(',')

			console.log(goodsIdStr)
			// 发请求
			this.queryGoodsList(goodsIdStr)
		},
		computed: {
			isAll: {
				get() {
					//所有的商品都勾选的话，全选勾选
					//1.勾选的商品数量===所有商品长度
					// let sum = 0
					// this.goodsList.forEach(item => {
					// 	if (item.checked) {
					// 		sum++
					// 	}
					// })
					// return sum === this.goodsList.length
					// 2.有一个商品不勾选的话,直接return false 
					//forEach无法中断
					// this.goodsList.forEach(item=>{
					// 	if(!item.checked){
					// 		return false
					// 	}
					// })
					// return true
					// for (let item of this.goodsList) {
					// 	// 如果有一个商品没有勾选的话,全选肯定没勾选
					// 	if (!item.checked) {
					// 		return false
					// 	}
					// }
					// return true
					//所有的商品都勾选的话，全选勾选 
					return this.goodsList.every(item => {
						return item.checked
					})
				},
				set(status) {
					console.log(status)
					// 所有的商品勾选状态和全选一致,和status一致
					this.goodsList.forEach(item => {
						item.checked = status
					})
				}
			},
			totalNum() {
				// 总数量:所有勾选的商品数量之和
				// let sum = 0
				// this.goodsList.forEach(item => {
				// 	// 如果商品是勾选的话,加上它的数量
				// 	if (item.checked) {
				// 		sum += item.num
				// 	}
				// })
				// return sum
				return this.goodsList.reduce((sum, item) => {
					return sum + (item.checked ? item.num : 0)
				}, 0)
			},
			totalPrice() {
				//总价格:所有勾选的商品数量*单价之和
				return this.goodsList.reduce((sum, item) => {
					return sum + (item.checked ? item.num * item.goods_price : 0)
				}, 0)
			}
		},
		watch: {
			goodsList: {
				handler() {
					console.log(this.goodsList)
					// 把goodsList的改变更新storage 
					//goodsList包含需要更新storage所有的数量
					this.$store.commit('updateCart',this.goodsList)
				},
				// 深度侦听
				deep: true
			}
		},
		methods: {
			// 跳转支付页面
			toPay() {
				// 如果没有添加商品,提示并返回
				if (!this.totalNum) {
					uni.showToast({
						title: '请添加商品先',
						icon: 'none'
					})
					return
				}
				// 判断是否登录
				let token = uni.getStorageSync('token')
				// 如果用户没有登录
				if (!token) {
					uni.navigateTo({
						url: '/pages/login/login'
					})
					// 后面的语句不需要执行
					return
				}

				uni.navigateTo({
					url: '/pages/pay/pay'
				})
			},
			// 全选取反
			toggleAll() {
				this.isAll = !this.isAll
			},
			// 商品数量--
			subNum(item, index) {
				// 如果数量为1的话,弹框,如果确定的话,删除这一项,return
				if (item.num === 1) {
					uni.showModal({
						title: '提示',
						content: '你确定要删除这个商品吗？',
						success: res => {
							if (res.confirm) {
								// 用户确定，删除这一项
								this.goodsList.splice(index, 1)
							}
						},
					});
					return
				}
				item.num--
			},
			// 商品数量++
			addNum(item) {
				item.num++
			},
			// 跳转到商详
			toItem(goodsId) {
				uni.navigateTo({
					url: '/pages/item/item?goodsId=' + goodsId
				})
			},
			// 商品勾选/反勾选
			toggleChecked(item) {
				item.checked = !item.checked
				// this.goodsList[index].checked =!this.goodsList[index].checked
			},
			async queryGoodsList(goodsIdStr) {
				// 如果storage cart是空的,没必要发请求了
				if (!goodsIdStr) {
					this.goodsList = [] //清空列表
					return
				}
				let _goodsList = await this.$request({
					url: '/api/public/v1/goods/goodslist?goods_ids=' + goodsIdStr
				})

				this.goodsList = this.storageCartArr.map(item => {
					// item.goodsId去goodsList找一个符合条件的对象,条件goodsId相等
					let targetGoods = _goodsList.find(goods => {
						return goods.goods_id === item.goodsId
					})
					return { ...targetGoods,
						...item
					}
				})

				// 对data属性改变,尽量一步到位
				// this.goodsList ={
				// 	name:'iphone11'
				// }

				// this.goodsList.color='red'
				// this.$set
			}
		}
	}
</script>

<style lang="less">
	.iconfont {
		font-size: 44rpx;
	}

	.title {
		height: 88rpx;
		display: flex;
		align-items: center;

		.iconfont {
			color: #999;
			margin: 6rpx 12rpx 0 32rpx;
		}
	}

	.goods-item {
		height: 206rpx;
		background-color: #fff;
		border-top: 1rpx solid #ddd;
		display: flex;
		box-sizing: border-box;
		align-items: center;

		.iconfont {
			color: #eb4450;
			margin: 0 30rpx;
		}

		image {
			width: 160rpx;
			height: 160rpx;
		}

		.right {
			flex: 1;
			display: flex;
			flex-direction: column;
			margin: 0 20rpx 0 18rpx;

			.btm {
				display: flex;
				margin-top: 40rpx;
				justify-content: space-between;

				.price {
					color: #eb4450;
					display: flex;

					>text {
						font-size: 24px;
					}
				}

				.goods-num {
					display: flex;
					align-items: center;

					button {
						width: 60rpx;
						height: 50rpx;
						box-sizing: border-box;
						display: flex;
						justify-content: center;
						align-items: center;
						border: 4rpx solid #666;
					}

					text {
						margin: 0 30rpx;
					}
				}
			}
		}
	}

	.goods-list {
		position: absolute;
		bottom: 98rpx;
		top: 88rpx;
		width: 100%;
		overflow: auto;
		padding-bottom: 60rpx;
		background-color: #f4f4f4;
	}

	.account {
		height: 98rpx;
		border-bottom: 1rpx solid #ddd;
		display: flex;
		position: absolute;
		left: 0;
		right: 0;
		bottom: 0;
		align-items: center;

		.iconfont {
			color: #eb4450;
			margin: 0 30rpx;
		}

		.account-btn {
			width: 230rpx;
			height: 98rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			color: #fff;
			background-color: #eb4450;
		}

		.select-all {
			flex: 1;
			display: flex;
			align-items: center;

			.price-wrapper {
				display: flex;
				flex: 1;
				justify-content: flex-end;
				align-items: center;
			}
		}

		.price {
			margin-right: 20rpx;

			.num {
				color: #eb4450;
			}

			.info {
				color: #999;
			}
		}
	}
</style>
